<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="栏目类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId">
              <j-dict-select-tag type="list" v-model="model.categoryId" dictCode="temple_donation_category,name,id" placeholder="请选择栏目类别" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="项目种类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="typeId">
              <j-dict-select-tag type="list" v-model="model.typeId" dictCode="temple_donation_type,name,id" placeholder="请选择项目种类" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-textarea v-model="model.description" rows="4" placeholder="请输入描述" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="优先级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="priority">
              <a-input-number v-model="model.priority" placeholder="请输入优先级" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="img">
              <imag-cropper v-model="model.img" :width="200" :height="150" ></imag-cropper>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="目标金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="targetAmount">
              <a-input-number v-model="model.targetAmount" placeholder="请输入目标金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beginDate">
              <j-date placeholder="请选择开始日期" v-model="model.beginDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endDate">
              <j-date placeholder="请选择结束日期" v-model="model.endDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否外部链接" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isLink">
              <j-switch v-model="model.isLink"  ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24"  v-if="model.isLink=='Y'">
            <a-form-model-item label="链接地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="url">
              <a-textarea v-model="model.url" rows="4" placeholder="请输入链接地址" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="model.isLink=='N'">
            <a-form-model-item label="内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <j-editor v-model="model.content" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="相关新闻" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="newsList">
                <j-select-news-by-category v-model="model.newsList" />
              <!--<j-editor v-model="model.newsList" />-->
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import JSelectNewsByCategory from '../../news/JSelectNewsByCategory'
  import ImagCropper from '../../../jeecg/ImagCropper'


  export default {
    name: 'TempleDonationForm',
    components: {
      JSelectNewsByCategory,
      ImagCropper
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           categoryId: [
              { required: true, message: '请输入栏目类别!'},
           ],
           typeId: [
              { required: true, message: '请输入项目种类!'},
           ],
           name: [
              { required: true, message: '请输入名称!'},
           ],
           description: [
              { required: true, message: '请输入描述!'},
           ],
           priority: [
              { required: false},
              { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},
           ],
           img: [
              { required: true, message: '请输入图片!'},
           ],
           targetAmount: [
              { required: true, message: '请输入目标金额!'},
              { pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
           ],
           beginDate: [
              { required: true, message: '请输入开始日期!'},
           ],
           endDate: [
              { required: true, message: '请输入结束日期!'},
           ],
           content: [
              { required: true, message: '请输入内容!'},
           ]
        },
        url: {
          add: "/temple.donation/templeDonation/add",
          edit: "/temple.donation/templeDonation/edit",
          queryById: "/temple.donation/templeDonation/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>